'''
Created on Aug 25, 2011

@author: nktran
'''

import re
import sys
import os

from fsa import FSA
from constant import LAMBDA

def format(string):
    """
    @return: new format of the string    
    """
    if string == LAMBDA:
        string = "_lambda_"        
    return string    

def read_file(fname, delim):
    """
    Return the content of the file namely fname with the delimiter
    
    @return: content of the file
    """
#    if fname not in os.listdir("."):
#        print fname, 'does not exist!!!'
#        sys.exit(1)        
    result = set()
    fin = open(fname, 'r')
    try:
        lines = re.split(delim, fin.read())
        for line in lines:
            if len(line.strip()) == 0: continue
            result.add(line.strip())
    finally:
        fin.close()
    
    return result            
    
def check(ext):
    """
    Check whether example is accepted by FSA
    
    @param ext: file extension 
    """
    acceptor = FSA()
    acceptor.init(ext)
    
    example = raw_input('Example need to be parsed: ')
    while True:
        if acceptor.parse(example):
            print '%s is accepted by FSA!\n' % (example)
        else:
            print '%s is rejected by FSA!\n' % (example)
        
        example = raw_input('Continue....(n/example)?')
        if example == 'n':
            break

def toString(input_set):
    """
    """
    return ' '.join(input_set) + "\n"

def log_file(fname, content):
    """
    """
    fout = open(fname, 'a')
    try:
        fout.write(content)
    finally:
        fout.close()
        

if __name__=="__main__":
    if len(sys.argv) < 2:
        print '\n usage: python utils.py [ext]'
        sys.exit(1)
    
    check(sys.argv[1])